package com.webinsky.publish.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.mail.MessagingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webinsky.common.proj.DelFlag;
import com.webinsky.common.utils.EmailUtils;
import com.webinsky.common.utils.EmailUtils.EmailContent;
import com.webinsky.common.utils.EmailUtils.EmailFrom;
import com.webinsky.common.utils.EmailUtils.EmailSent;
import com.webinsky.common.utils.EmailUtils.EmailSmtpServre;
import com.webinsky.publish.domain.Info;
import com.webinsky.publish.domain.InfoFilter;
import com.webinsky.publish.domain.Infos;
import com.webinsky.publish.persist.dao.EmailSettingMapper;
import com.webinsky.publish.persist.dao.InfoEntityMapper;
import com.webinsky.publish.persist.entity.EmailSetting;
import com.webinsky.publish.persist.entity.InfoEntity;
import com.webinsky.publish.service.InfoManagement;

@Service
public class InfoManagementImpl implements InfoManagement {
	private static final Logger logger = LoggerFactory.getLogger(InfoManagementImpl.class);
	@Autowired
	private InfoEntityMapper infoEntityDao;
	@Autowired
	private EmailSettingMapper emailDao;
	
	
	private InfoEntity convert(Info info) {
		InfoEntity entity = new InfoEntity();
		entity.setContent(info.getContent());
		entity.setEmail(info.getEmail());
		entity.setCompany(info.getCompany());
		entity.setAddress(info.getAddress());
		entity.setRemark(info.getRemark());
		entity.setInfoDate(info.getInfoDate());
		entity.setName(info.getName());
		entity.setSiteId(info.getSiteId());
		entity.setTelphone(info.getMobile());
		entity.setSiteId(info.getSiteId());
		return entity;
	}
	
	private Info convert(InfoEntity entity) {
		Info info = new Info();
		info.setContent(entity.getContent());
		info.setEmail(entity.getEmail());
		info.setCompany(entity.getCompany());
		info.setAddress(entity.getAddress());
		info.setRemark(entity.getRemark());
		info.setId(entity.getId());
		info.setInfoDate(entity.getInfoDate());
		info.setMobile(entity.getTelphone());
		info.setName(entity.getName());
		info.setSiteId(entity.getSiteId());
		return info;
	}
	
	@Override
	public void submitInfo(Info info) {
		if ( info != null ) {
			InfoEntity entity = convert(info);
			entity.setDel(DelFlag.UNDel.getFlag());
			infoEntityDao.insert(entity);
			info.setId(entity.getId());
			// 发送邮件
			List<EmailSetting> settings = emailDao.selectBySiteId(entity.getSiteId());
			if ( settings.isEmpty() ) {
				logger.info("{} submitInfo siteid = " + info.getSiteId() + " 邮件信息没有设置");	
				return;
			}
			EmailSetting setting = settings.get(0);
			EmailContent content = new EmailContent();
			EmailFrom from = new EmailFrom();
			from.formName = setting.getSender();
			from.fromEMail = setting.getSender();
			content.from = from;
			EmailSent sent = new EmailSent();
			StringBuilder contentHtml = new StringBuilder();
			contentHtml.append("姓名：" + info.getName() + "<br />");
			contentHtml.append("手机：" + info.getMobile() + "<br />");
			if ( info.getEmail() != null ) {
				contentHtml.append("邮件：" + info.getEmail() + "<br />");
			}
			contentHtml.append("公司：" + info.getCompany() + "<br />");
			if ( info.getAddress() != null ) {
				contentHtml.append("地址：" + info.getAddress() + "<br />");
			}
			String remarker = "";
			if ( info.getContent() != null ) {
				remarker = info.getContent();
			}
			if ( info.getRemark() != null ) {
				remarker = remarker + info.getRemark();
			}
			contentHtml.append("留言：" + remarker + "<br />");
			sent.body = contentHtml.toString();
			sent.recievers = setting.getRecievers().split(";");
			sent.subject = info.getSiteName() + "咨询";
			content.sender = sent;
			EmailSmtpServre server = new EmailSmtpServre();
			server.smtpIsSSL = true;
			server.smtpPassword = setting.getPassword();
			server.smtpPort = setting.getPort();
			server.smtpServer = setting.getSmtp();
			server.smtpUserName = setting.getSender();
			content.smtpServer = server;
			
			try {
				EmailUtils.sendMail(content);
			} catch (UnsupportedEncodingException e) {
				logger.error("{} submitInfo 发送是失败", e);
			} catch (MessagingException e) {
				logger.error("{} submitInfo 发送是失败", e);
			}
			
		}
	}

	@Override
	public Infos getByFilter(InfoFilter filter) {
		Infos infos = new Infos();
		if ( filter != null ) {
			List<InfoEntity> entities = infoEntityDao.selectByFilter(filter);
			if ( entities.isEmpty() ) {
				return infos;
			}
			entities.forEach(e -> infos.getInfos().add(convert(e)));
			infos.setTotal(infoEntityDao.selectCountByFilter(filter));
		}
		return infos;
	}

	@Override
	public void deleteInfo(int infoId) {
		if ( infoId > 0 ) {
			infoEntityDao.deleteById(infoId);
		}
	}

	@Override
	public int getCountBySiteId(int siteId) {
		if ( siteId <= 0 ) {
			return 0;
		}
		return infoEntityDao.selectCountBySiteId(siteId);
	}

}
